package com.zjc.code.algorithm.my;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-24  11:55
 */
public class Code05_BSAwesome {
    // 寻找数组(无序，相邻元素不同)中的任意一个局部最小值(比左右两个数都小)索引
    public static int getLessIndex(int[] arr) {
        // 特殊情况处理
        if (arr == null || arr.length == 0) {
            return -1;
        }
        // 边界处理,因为边界值只有左边或右边
        if (arr.length == 1 || arr[0] < arr[1]) {
            return 0;
        }
        if (arr[arr.length - 1] < arr[arr.length - 2]) {
            return arr.length - 1;
        }

        int left = 1;
        int right = arr.length - 2;
        int mid = 0;
        while (left < right) {
            mid = left + ((right - left) >> 1);
            System.out.println("mid:" + mid);
            if (arr[mid] > arr[mid - 1]) {//比左边大，找左边
                right = mid - 1;
            } else if (arr[mid] > arr[mid + 1]) {//比右边大，找右边
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] arr = {3, 2, 6, 4, 3, 5, 1, 4};
        System.out.println(getLessIndex(arr));
    }

}
